/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sample.controller.healthcheck;

import com.lvyh.lightframe.healthcheck.cache.ApplicationAvailabilityCache;
import com.lvyh.lightframe.healthcheck.common.ErrorCode;
import com.lvyh.lightframe.healthcheck.common.HealthCheckResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.LivenessState;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 1、Use application availability to get the status of the application.
 * 2、Publish the availability change event to update the status.
 *
 * @author lvyh 2021/03/21.
 */
@RestController
@Slf4j
public class CustomHealthCheckController {

    @Autowired
    private ApplicationEventPublisher eventPublisher;
    @Autowired
    private ApplicationAvailabilityCache applicationAvailabilityCache;

    //http://localhost:8080/custom-health-check
    @GetMapping("custom-health-check")
    public HealthCheckResult customHealthCheck() {
        return new HealthCheckResult(ErrorCode.SUCCESS.getStatus(), "ok", applicationAvailabilityCache.toString());
    }

    //http://localhost:8080/readiness-accepting
    @GetMapping("/readiness-accepting")
    public String markReadinesAcceptingTraffic() {
        AvailabilityChangeEvent.publish(eventPublisher, this, ReadinessState.ACCEPTING_TRAFFIC);
        return "Readiness marked as accepting_traffic";
    }

    //http://localhost:8080/readiness-refuse
    @GetMapping("/readiness-refuse")
    public String markReadinesRefusingTraffic() {
        AvailabilityChangeEvent.publish(eventPublisher, this, ReadinessState.REFUSING_TRAFFIC);
        return "Readiness marked as refusing_traffic";
    }

    //http://localhost:8080/liveness-correct
    @GetMapping("/liveness-correct")
    public String markLivenessCorrect() {
        AvailabilityChangeEvent.publish(eventPublisher, this, LivenessState.CORRECT);
        return "Liveness marked as correct";
    }

    //http://localhost:8080/liveness-broken
    @GetMapping("/liveness-broken")
    public String markLivenessBroken() {
        AvailabilityChangeEvent.publish(eventPublisher, this, LivenessState.BROKEN);
        return "Liveness marked as broken";
    }

}
